package com.qf.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.*;
import com.qf.service.UserService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController   // 表示当前类是一个控制器类，返回json数据
@CrossOrigin // 跨域注解
public class UserController {

    // 注入业务层
    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param user
     */
    @PostMapping("/user/login")
    public R login(@RequestBody User user) {
        System.out.println("user = " + user);
        String token = userService.login(user);
        return R.success(token);
    }

    //根据用户名查询用户信息
    @GetMapping("/user/selectByUsername")
    public R selectByUsername(@RequestParam String username) {
        User user = userService.selectByUsername(username);
        return R.success(user);
    }


    @PreAuthorize("hasAuthority('system:user:list')")
    @GetMapping("/user/list")
    public R list( UserDTO userDTO){

        System.out.println(userDTO);
        // 开始
        PageHelper.startPage(userDTO.getPageNum(),userDTO.getPageSize());

        List<UserVO> list = userService.list(userDTO);
        System.out.println("list = " + list);
        // 分页信息
        PageInfo<UserVO> pageInfo = new PageInfo<>(list);

        return R.success(pageInfo);
    }

    @PreAuthorize("hasAuthority('system:user:del')")
    //按照id进行删除
    @DeleteMapping("/user/delete")
    public R delete(@RequestParam("id") int id){
        userService.delete(id);
        return R.success();
    }


    @PreAuthorize("hasAuthority('system:user:del')")
    //批量删除--按照list删除
    @GetMapping("/user/deleteAll")
    public R deleteAll(@RequestParam List<Integer> id){
        userService.deleteAll(id);
        return R.success();
    }

    //添加数据
    @PreAuthorize("hasAuthority('system:user:add')")
    @PostMapping("/user/add")
    public R add(@RequestBody UserVO userVO) {
        userService.add(userVO);
        return R.success();
    }

    //修改数据
    @PreAuthorize("hasAuthority('system:user:edit')")
    @PostMapping("/user/update")
    public R update(@RequestBody UserVO uservo){
        System.out.println(uservo);
        int update = userService.update(uservo);
        if (update > 0){
            return R.success();
        }
        return R.fail();
    }


    //查询所有的权限
    @GetMapping("/user/selectPermission")
    public R selectPermission() {
        List<RolePermission> list = userService.selectPermission();
        return R.success(list);
    }


    //添加用户以及对应权限
//    @PreAuthorize("hasAuthority('system:user:add')")
//    @PostMapping("/user/addUserAndPermission")
//    public R addUserAndPermission(@RequestBody User user){
//        int add = userService.addUserAndPermission(user);
//        if (add > 0){
//            return R.success();
//        }
//        return R.fail();
//    }





}